#include "stm32f10x.h" //STM32头文件
#include "menu.h"
#include "delay.h"
#include "oled.h"
#include "TM1638.h"
#include "usart.h"
#include "ds18b20.h"
#include "bmp.h"
#include "buzzer.h"
#include "Lcd_Driver.h"
#include "tim.h"
//static u8 last_index=0;//上一个界面索引值
static u8 func_index2 = 0; 
extern unsigned int TEMP;
extern unsigned int SETTEMP;
extern unsigned int PWM;
extern  int tmr1min;
u8 airc=OFF,mode=1,fanc=OFF,timec=OFF;
u8 flag1min=0;
static int flagxmin=0;
unsigned int  set_temp;
 unsigned char flag800ms;
extern u8 flag100ms;
extern u8 timer4_state;
static void (*current_operation_index)();
static int up_val, down_val, enter_val,back_val;
u8 byte[4]={0};//定义数组
u8 t_test=0;	//四位发送


key_table  code_table[10]=
{ 	
//	current, up向上翻索引号 ,down向下翻索引号, enter确认索引号	,back返回索引号，(*current_operation)(); 
	  {0,0,0,1,0, mainui},  //主界面 
  	{1,1,2,6,0,mainui2},  //风扇主  
  	{2,1,3,4,0,mainui3},  //灯主 
    {3,2,7,5,0,mainui4},  //空调主 		
		{4,4,4,4,2,light_Child}, //灯子
		{5,5,5,5,3,air_conditioner_Child},//空调子
		{6,6,6,6,1,efan},   //风扇子
		{7,3,7,8,0,mainui6},   //about
		{8,8,8,8,7,aboutme},
};



void efan()
{
   static u8 a;
	 a=an();
			switch(a)
		{
			case 1: fanc==0?fanc=4:fanc--;
					break;
			case 2:fanc==4?fanc=0:fanc++;
					break;
			default:break;
		}
		/*设置风扇档位**************/				
//     if(cur_poss==2&&fanc==1){
//			if(a==7)
//						 {
//							 set_temp--;
//						 }
//						else if(a==8&&fanc==1) 
//							{
//								 set_temp++;
//							}
//				}	
//		OLED_Showall(0,0,lih?"1.风扇:ON":"1.风扇:OFF",16,1);
		OLED_Showall(0,20,"1.档位:",16,1);
//		OLED_Showall(0,20,"3.温度:",16,cur_pos==2?0:1);
	  OLED_ShowNum(70,20,(fanc),1,16,0);
//	 OLED_Showall(86,20,"℃",16,cur_pos==2?0:1);
		OLED_Refresh();
}
/*
函数功能：灯选项子菜单
参数：
返回值：无
*/
void light_Child()
{
	static u8 a,cur_pos=1;
	static u8 lih=OFF,lih2=OFF,lih3=OFF,lih4=OFF,lih5=OFF,lih6=OFF;
	TM1638_Display_LED(0,lih);TM1638_Display_LED(1,lih2);
	TM1638_Display_LED(2,lih3);TM1638_Display_LED(3,lih4);
	TM1638_Display_LED(4,lih5);TM1638_Display_LED(5,lih6);
	 a=an();
		switch(a)
		{
			case 1: cur_pos==1?cur_pos=6:cur_pos--;
					break;
			case 3://确定(设置)按键
			{
//				an();
				switch(cur_pos)
				{
					case KEY_PREVIOUS:if(a==3)lih=!lih;
//					if(lih==ON){USART2_printf("The light1 is on");}
//					 else{USART2_printf("The light1 is off");}
							break;
					case 2:if(a==3)lih2=!lih2;
							break;
					case 3:if(a==3)lih3=!lih3;
							break;
					case 4:if(a==3)lih4=!lih4;
							break;
					case 5:if(a==3)lih5=!lih5;
              break;
					case 6:if(a==3)lih6=!lih6;
              break;
					default:break;
				}
			}
					break;
			case 2:cur_pos==6?cur_pos=1:cur_pos++;
					break;
			default:break;
			if(lih==ON)
		{BUZZER_BEEP1();}
		}
		if(cur_pos<=3)
		{
			 OLED_Showall(0,0,lih?"1.灯1:ON":"1.灯1:OFF",16,cur_pos==1?0:1);
			 OLED_Showall(0,20,lih2?"2.灯2:ON":"2.灯2:OFF",16,cur_pos==2?0:1);
			 OLED_Showall(0,40,lih3?"3.灯3:ON":"3.灯3:OFF",16,cur_pos==3?0:1);
			 OLED_ShowPicture(116,0,12,54,BMP13,1);	
		}
		else if(cur_pos<=6)
		{
			 OLED_Showall(0,0,lih4?"4.灯4:ON":"4.灯4:OFF",16,cur_pos==4?0:1);
			 OLED_Showall(0,20,lih5?"5.灯5:ON":"5.灯5:OFF",16,cur_pos==5?0:1);
			 OLED_Showall(0,40,lih6?"6.灯6:ON":"6.灯6:OFF",16,cur_pos==6?0:1);
			 OLED_ShowPicture(116,10,12,54,BMP14,1);
		}	
	  OLED_Refresh();
}
void air_conditioner_Child()//空调
{
	static u8 a,cur_pos=1,cur_pos2=1,cur_pos3=1,cur_pos4=1,x1=0,time_flag ;
  static u8 timeingtime=0,time_temp=0;
	static int time=0;
	  a=an();
		switch(a)
		{
			case 1: cur_pos==1?cur_pos=8:cur_pos--;
					break;
			case 3://确定(设置)按键
			{
//				an();
				switch(cur_pos)
				{
					case 1:if(a==3)airc=!airc;
							break;
					case 2:if(a==3)mode=!mode;
							break;
					case 3:if(a==3)cur_pos2=!cur_pos2;
							break;
					case 4:if(a==3)timec=!timec; time_flag=1;
							break;
					case 5:if(a==3) cur_pos3=!cur_pos3;
							break;
	        case 6:if(a==3) cur_pos4=!cur_pos4;
							break;                
							break;
					default:break;
				}
			}
					break;
			case 2:cur_pos==8?cur_pos=1:cur_pos++;
					break;
			default:break;
		}
/*温度限制*******/
		 if(mode==ON)
		 {
		   set_temp<=15?set_temp=26:set_temp;
			 set_temp>=27?set_temp=22:set_temp;
		 }
		else if(mode==OFF) 
			{
		     set_temp<=28?set_temp=40:set_temp;
				 set_temp>=41?set_temp=40:set_temp;
		  }
/*设置温度**************/				
     if(cur_pos==3&&cur_pos2==0){
			 if(a==7)
						 {
							 set_temp--;
						 }
				else if(a==8) 
							{
								 set_temp++;
							}
				}
/*设置定时时间**************/				
     if(cur_pos==5&&cur_pos3==0){
				if(a==7)
						 {
							 time==0?time=6:time--;
						 }
				else if(a==8) 
							{
						  	time==6?time=0:time++;
							}
				}		 
/*设置定时升降温度**************/				
     if(cur_pos==6&&cur_pos4==0){
				if(a==7)
						 {
							 time_temp==0?time_temp=6:time_temp--;
						 }
				else if(a==8) 
							{
						  	time_temp==6?time_temp=0:time_temp++;
							}
				}		
/*定时中断******设置定时升降温度**************/				
     if(timec==ON&&airc==ON&&x1==0){
			     
			   if(flag1min){
					     flag1min=0;
							 flagxmin++;
							 if(time==(flagxmin-1)){//因为on到off的判断，开始的flagxmin置1了，所以要-1
								  flagxmin=0;
								  if(mode==1){ set_temp=(set_temp+time_temp);}
				          if(mode==0){ set_temp=(set_temp-time_temp);}
									x1=1;
								}
							}
				        
				}	 
		
/*定时功能由on到off的判断********************/			
		 if(time_flag==1){
							 if(timec==0){//由on到off							 
								 time_flag=0;
								 x1=0;
								}
							 if(timec==1){//由off到on
								 tmr1min=0;
								 flag1min=0;
								 time_flag=0;
								 x1=0;
								}
							}
				        
		 
		if(airc==ON)
				{
					air_pid();
				}				
if(cur_pos<=3)
		{		 
	 OLED_Showall(0,0,airc?"1.空调:开":"1.空调:关",16,cur_pos==1?0:1);
   OLED_Showall(0,20,mode?"2.模式:制冷":"2.模式:制热",16,cur_pos==2?0:1);
   OLED_Showall(0,40,"3.温度:",16,cur_pos==3?0:1);
	 OLED_ShowNum(70,40,(set_temp),2,16,cur_pos2);
	 OLED_Showall(86,40,"℃",16,cur_pos2);	
	 OLED_ShowPicture(116,0,12,54,BMP13,1);	
		}
		else if(cur_pos>=4&&cur_pos<=6)
		{
			  OLED_Showall(0,0,timec?"4.定时升降温:开":"4.定时升降温:关",16,cur_pos==4?0:1);
				OLED_Showall(0,20,"5.时间:",16,cur_pos==5?0:1);
				OLED_ShowNum(70,20,(time),1,16,cur_pos3);
				OLED_Showall(78,20,"分钟",16,cur_pos3);
			  OLED_Showall(0,40,"6.升降温",16,cur_pos==6?0:1);
			  OLED_ShowNum(75,40,(time_temp),1,16,cur_pos4);
			  OLED_Showall(86,40,"℃",16,cur_pos4);	
		}
else if(cur_pos==7)
		{
			  
		    OLED_ShowNum(20,23,(DS18B20_Get_Temp()/10%100),2,16,1);
	      OLED_Showall(37,23,".",16,1);
			  OLED_ShowNum(43,23,(DS18B20_Get_Temp()/1%10),1,16,1);
			  OLED_Showall(52,23,"℃",16,1);
		    OLED_ShowNum(20,4,(PWM),4,16,1);
			  OLED_Showall(52,4,"/9999",16,1);
//			   OLED_ShowNum(65,4,(TEMP),4,16,1);
			  OLED_ShowNum(20,45,(SETTEMP/10%100),2,16,1);
			  OLED_Showall(36,45,"℃",16,1);
		    OLED_ShowNum(70,45,(tmr1min),4,16,1);
				OLED_ShowNum(75,23,(time),1,16,1);
				OLED_ShowNum(85,23,(flagxmin),1,16,1);
			  OLED_ShowPicture(116,10,12,54,BMP14,1);	
		}
else if(cur_pos==8)
{
	static int x=0;
	
	if(flag100ms)										
		{
			flag800ms++;
			if(flag800ms==20)                
			{
				flag800ms=0;
				x++;if(x>=128)x=0;
//	LCD_DrawPoint(x,800-(TEMP/2));
	 OLED_DrawPoint(x,32+((SETTEMP-TEMP)/10%100),1);
			}
		}	
	
	
	//画点 
//x:0~127
//y:0~63
//t:1 填充 0,清空	

   

}	

	  OLED_Refresh();

}
void aboutme()
{
     	 OLED_Showall(40,22,"杨志轩",16,1);
	     OLED_Refresh();
}	
//	//按键管脚定义
//	pinMode(keyup,INPUT_PULLUP); //上拉输入模式
//	pinMode(keydown,INPUT_PULLUP);
//	pinMode(keyenter,INPUT_PULLUP);
//}

void mainui()
{
//     OLED_ShowString(53,36,"zzzzz",16,1);
	    OLED_Showall(42,1,"主菜单",16,1);
			OLED_DrawLine(0,17,128,17,1);
			OLED_DrawLine(0,0,128,0,1);
			OLED_DrawLine(0,1,0,17,1);
			OLED_DrawLine(127,1,127,17,1);
	    OLED_ShowPicture(0,48,16,16,BMP9,1);  
	    OLED_ShowPicture(36,48,16,16,BMP10,1);
			OLED_ShowPicture(75,48,16,16,BMP11,1);
			OLED_ShowPicture(112,48,16,16,BMP12,1);//四个按键
		  OLED_Refresh();

}

void mainui2()
{
	    OLED_ShowPicture(40,9,48,48,BMP16,1);
	    OLED_ShowPicture(107,15,21,35,BMP5,1);
		  OLED_Refresh();

}
void mainui3()//灯
{
	   OLED_ShowPicture(0,15,21,35,BMP4,1);
	   OLED_ShowPicture(107,15,21,35,BMP5,1);
	   OLED_ShowPicture(40,9,48,48,BMP6,1);
//     OLED_Showall(60,30,"灯",16,1);
		 OLED_Refresh();

}
void mainui4()//空调
{
	   OLED_ShowPicture(0,15,21,35,BMP4,1);
	   OLED_ShowPicture(107,15,21,35,BMP5,1);
     OLED_ShowPicture(40,9,48,48,BMP8,1);
		 OLED_Refresh();
}


void mainui6()
{
	  OLED_ShowPicture(0,15,21,35,BMP4,1);
    OLED_ShowPicture(40,9,48,48,BMP15,1);
    OLED_Refresh();

}
unsigned char an()
			{	
				
			//上选择键	
				while(TM1638_ReadKey()==1) 
				{	
					delay_ms(25);
					while(TM1638_ReadKey()==1);		
					return KEY_PREVIOUS;			
				}  
			//下选择键
				while(TM1638_ReadKey()==2) 
				{	
					delay_ms(25);
					while(TM1638_ReadKey()==2);		
					return KEY_NEXT;			
				} 
			//确认键
				while(TM1638_ReadKey()==3) 
				{	
					delay_ms(25);
					while(TM1638_ReadKey()==3);
					return KEY_ENTER;
				}  
			//返回键
				while(TM1638_ReadKey()==4) 
				{	
					delay_ms(25);
					while(TM1638_ReadKey()==4);		
					return KEY_BACK;
				}  
					while(TM1638_ReadKey()==7) 
				{	
					delay_ms(25);
					while(TM1638_ReadKey()==7);		
					return 7;
				}  
				while(TM1638_ReadKey()==8) 
				{	
					delay_ms(25);
					while(TM1638_ReadKey()==8);		
					return 8;
				} 
			 	return 0;
			}

void loop()
{
	if(TM1638_ReadKey()==1){up_val=1;}else up_val=0;
	if(TM1638_ReadKey()==2){down_val=1;}else down_val=0;
	if(TM1638_ReadKey()==3){enter_val=1;}else enter_val=0;
  if(TM1638_ReadKey()==4){back_val=1;}else back_val=0;
	if((up_val==1)||(down_val==1)||(enter_val==1)||(back_val==1)) 		
	{ 			
		delay_ms(20);//消抖 			
		if(up_val==1) 		
		{ 
      		while(TM1638_ReadKey()==1)//松手检 
      		{
//						last_index=func_index2;
//        		if(TM1638_ReadKey()==1){up_val=1;}else up_val=0;
        		current_operation_index=code_table[func_index2].current_operation;     
        		(*current_operation_index)();//执行当前操作函数
      		}
      		OLED_Clear();
      		func_index2=code_table[func_index2].up; //向上     
		} 
    			
		if(down_val==1) 		 	
		{ 	
			while(TM1638_ReadKey()==2)//松手检 
      		{
//        			if(TM1638_ReadKey()==2){down_val=1;}else down_val=0;
		        current_operation_index=code_table[func_index2].current_operation;     
		        (*current_operation_index)();//执行当前操作函数
     		 }
	      OLED_Clear();
	      func_index2=code_table[func_index2].down; //向下翻  			
		}
    		 
		if(enter_val==1) 			
		{ 
		      while(TM1638_ReadKey()==3)//松手检 
		      {
//		        if(TM1638_ReadKey()==3){enter_val=1;}else enter_val=0;
		        current_operation_index=code_table[func_index2].current_operation;     
		        (*current_operation_index)();//执行当前操作函数
     		  }
		      OLED_Clear();
		      func_index2=code_table[func_index2].enter; //确认      			
		} 
    if(back_val==1) 			
		{ 
		      while(TM1638_ReadKey()==4)//松手检 
		      {
//		        if(TM1638_ReadKey()==4){back_val=1;}else enter_val=0;
		        current_operation_index=code_table[func_index2].current_operation;     
		        (*current_operation_index)();//执行当前操作函数
     		  }
		      OLED_Clear();
		      func_index2=code_table[func_index2].back; //确认      			
		}		
	}
	current_operation_index=code_table[func_index2].current_operation; 		
	(*current_operation_index)();//执行当前操作函数
 } 
